import 'dart:ffi';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:provider/provider.dart';
import 'package:s_k_todo/provider/home_provider.dart';
import 'package:s_k_todo/provider/task_list.dart';
import 'package:s_k_todo/ui/task_add_page.dart';
import 'package:s_k_todo/ui/task_info_page.dart';
import 'package:s_k_todo/widget/base_page.dart';

import '../model/task_model.dart';
import '../r.g.dart';
import 'home_main.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends BaseState<HomePage> {
  @override
  void initState() {
    super.initState();
    context.read<TaskListProvider>().getTaskList();
  }

  @override
  Widget pageBuilder(BuildContext context, PageType pageType) {
    HomeProvider homeProvider = context.watch<HomeProvider>();
    TaskListProvider taskListProvider = context.watch<TaskListProvider>();
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () async {
          TaskInfo? data = await showModalBottomSheet(
              context: context,
              builder: (ctx) {
                return const TaskAddPage();
              },
              isScrollControlled: true);
          if (data != null) taskListProvider.addNewTask(data);
        },
        child: Icon(homeProvider.isAddNew ? Icons.upload : Icons.add),
      ),
      body: _PageBody(type: pageType),
      bottomNavigationBar: pageType != PageType.phone
          ? null
          : BottomNavigationBar(
              items: const [
                  BottomNavigationBarItem(
                      icon: Icon(Icons.today), label: "Today"),
                  BottomNavigationBarItem(
                      icon: Icon(Icons.history), label: "History"),
                  BottomNavigationBarItem(
                      icon: Icon(Icons.bubble_chart), label: "Statistics"),
                ],
              currentIndex: 0,
              onTap: (index) {
                print("index: $index");
              }),
    );
  }
}

class _PageBody extends StatelessWidget {
  PageType type;

  _PageBody({super.key, required this.type});

  @override
  Widget build(BuildContext context) {
    TaskListProvider taskListProvider = context.watch<TaskListProvider>();
    Widget widget;
    if (type == PageType.phone) {
      widget = IndexedStack(
        index: 0,
        children: [HomeMainBody(type)],
      );
    } else {
      widget = ClipRRect(
        clipBehavior: Clip.none,
        child: Row(
          mainAxisAlignment: MainAxisAlignment.start,
          children: [
            Container(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  const SizedBox(
                    height: 40,
                  ),
                  Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Container(
                        height: 60,
                        width: 60,
                        child: CircleAvatar(
                          backgroundColor: Theme.of(context).primaryColor,
                          foregroundImage: NetworkImage(
                              "http://i.juweishe.com/20220301/aa6635279459f9e20b283d104db92041.jpg"),
                        ),
                      ),
                      const SizedBox(
                        height: 10,
                      ),
                      const Text("遥远的喵",
                          style: TextStyle(fontWeight: FontWeight.w600),
                          overflow: TextOverflow.ellipsis),
                    ],
                  ),
                  const SizedBox(
                    height: 40,
                  ),
                  Expanded(
                      child: _VerticalNavigation(
                    type: type,
                  )),
                ],
              ),
            ),
            const VerticalDivider(
              color: Color(0XFFF5F5F5),
            ),
            type == PageType.desktop
                ? SizedBox(
                    width: 650,
                    child: HomeMainBody(type),
                  )
                : Expanded(
                    child: Container(
                      alignment: Alignment.center,
                      child: ConstrainedBox(
                        constraints: const BoxConstraints(maxWidth: 650),
                        child: HomeMainBody(type),
                      ),
                    ),
                  ),
            Expanded(
              flex: type == PageType.desktop ? 3 : 0,
              child: taskListProvider.curTaskId == null
                  ? Container()
                  : TaskInfoPage(taskListProvider.curTaskId!),
            )
          ],
        ),
      );
    }
    return widget;
  }
}

class _VerticalNavigation extends StatelessWidget {
  PageType type;

  _VerticalNavigation({super.key, required this.type});

  @override
  Widget build(BuildContext context) {
    return NavigationRail(destinations: const [
      NavigationRailDestination(icon: Icon(Icons.today), label: Text("today")),
      NavigationRailDestination(
          icon: Icon(Icons.history), label: Text("History")),
      NavigationRailDestination(
          icon: Icon(Icons.bubble_chart), label: Text("Statistics")),
      NavigationRailDestination(
          icon: Icon(Icons.settings_outlined), label: Text("Setting"))
    ], selectedIndex: 0);
  }
}
